package com.xsd.juc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Description: juc
 * @Author: zhoufuzong
 * @Created: 2020-12-27 19:56:02
 * @Version: 1.1.0
 */


class AirConditioner {  // 资源类
    private  int number =0;

    private Lock lock = new ReentrantLock();

    private Condition condition = lock.newCondition();

    public    void increment() throws InterruptedException {

        lock.lock();

        // 1 判断
        try {
            while (number != 0) {  // while 唤醒时重新进入判断 避免虚假唤醒
                // if （number!=0) 时
                // 虚假唤醒： 此时另一个线程number ++ 进来了,
                // A wait ,C 抢到了，也进来了，---- A C wait , B C 在锁外
                // 下一次B 进来，消费掉了1 ，执行减法，number =0 然后notifyAll ， 重新被唤醒时， 0!=0 没有进入if 判断, A  C 都执行了 ++ ,
                condition.await(); // this.wait
            }

            //2 干活
            number++;

            System.out.println(Thread.currentThread().getName()+"\t"+number);

            // 3 通知
            condition.signalAll();  // this.notifyAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }


    }

    public  void decrement() throws InterruptedException {
        lock.lock();

        // 1 判断
        try {
            while (number == 0) {  // while 唤醒时重新进入判断 避免虚假唤醒
                condition.await(); // this.wait
            }

            //2 干活
            number--;

            System.out.println(Thread.currentThread().getName()+"\t"+number);

            // 3 通知
            condition.signalAll();  // this.notifyAll();

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}


/**
 * 题目： 现在两个线程，可以操作初始值为零的一个变量
 * 实现一个线程对该变量加1，一个线程对该变量减1，
 * 实现交替，来10轮，变量初始值为零
 *
 * 1  高内聚，低耦合前提下，线程操作资源类
 * 2  判断/干活/通知
 *
 * 3 多线程交互中，必须要防止多线程的虚假唤醒, 也即判断只能用while ,不能用if
 *
 */


public class ThreadWaitNotifyDemo {

    public static void main(String[] args) throws Exception {

        AirConditioner airConditioner = new AirConditioner();

        new Thread(()->{
            for (int i = 0; i <10 ; i++) {
                try {

                    airConditioner.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        },"A").start();

        new Thread(()->{for (int i = 0; i <10 ; i++) {
            try {

                airConditioner.decrement();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        } },"B").start();


        new Thread(()->{
            for (int i = 0; i <10 ; i++) {
                try {
                    airConditioner.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        },"C").start();

        new Thread(()->{for (int i = 0; i <10 ; i++) {
            try {
                airConditioner.decrement();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        } },"D").start();
    }
}
